package com.example.mofei.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements ItemDragListener {
    private RecyclerView recyclerView;
    private List<String>  contentList;
    private Button btnAdd;
    RecyclerViewAdapter recyclerViewAdapter=null;
    private ItemTouchHelper touchHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnAdd=(Button)findViewById(R.id.btnAdd);
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                recyclerViewAdapter.addItem("test",0);
                recyclerView.scrollToPosition(0);
            }
        });
        contentList=new ArrayList<>();
        for (int i = 0; i < 60; i++) {
            contentList.add("item"+i);
        }

        recyclerView=(RecyclerView)findViewById(R.id.recyclerView);
        //设置分割线
        recyclerView.addItemDecoration(new RecyclerDecoration(this,LinearLayoutManager.VERTICAL));
//        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));
        recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        recyclerViewAdapter=new RecyclerViewAdapter(contentList,this);
        recyclerView.setAdapter(recyclerViewAdapter);
//        recyclerView.addItemDecoration();

        RecyclerViewAdapter.OnItemClickListener itemClickListener=new RecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void clickListener(View view, int position) {
                Toast.makeText(MainActivity.this,"第"+(position+1)+"个被点击了",Toast.LENGTH_SHORT).show();
            }
        };
        recyclerViewAdapter.setOnItemClickListener(itemClickListener);

        //设置允许上下拖动
        touchHelper= new ItemTouchHelper(new ItemTouchCallback(recyclerViewAdapter));
        touchHelper.attachToRecyclerView(recyclerView);

    }

    @Override
    public void onStartDrags(RecyclerView.ViewHolder viewHolder) {
        touchHelper.startDrag(viewHolder);
    }
}
